function serializeData(data) {
	// If this is not an object, defer to native stringification.
	if (!angular.isObject(data)) {
		return ((data == null) ? "" : data.toString());
	}

	var buffer = [];

	// Serialize each key in the object.
	for (var name in data) {
		if (!data.hasOwnProperty(name)) {
			continue;
		}

		var value = data[name];

		buffer.push(
			encodeURIComponent(name) + "=" + encodeURIComponent((value == null) ? "" : value)
		);
	}

	// Serialize the buffer and clean it up for transportation.
	var source = buffer.join("&").replace(/%20/g, "+");
	return (source);
}

angular.module('CourseApp.controllers.Main', [])
	.controller('MainController', function($scope, $rootScope, $state, $stateParams, $cookieStore, $animate, $window, StudentService, StudentCourseService, toastr, LocateInfoService, QuestionInfoService, localStorageService) {

		// 绑定变量
		$scope.$watch(function() {
			return $rootScope.curcourse;
		}, function() {
			$scope.curcourse = $rootScope.curcourse;
		}, true);

		$scope.$back = function() {
			window.history.back();
		};
		// $scope.clicked = false;
		$scope.login = function() {
			// $scope.clicked = true;

			console.log(this.userid);
			console.log(this.password);
			StudentService.authenticate(serializeData({
				stuNum: this.userid,
				stuPwd: this.password
			}), function(authenticationResult) {
				console.log(authenticationResult);
				if (authenticationResult.success) {
					// 成功
					var authToken = authenticationResult.object.token;
					$rootScope.authToken = authToken;

					localStorageService.set('authToken', authToken);
					console.log(localStorageService.get('authToken'));
					StudentService.get(function(student) {
						$rootScope.student = student;
						localStorageService.set('student', student);
						console.log(student);

						toastr.success('登陆成功');
						
						$window.location.reload();
					});
				} else {
					toastr.error(authenticationResult.message);

					// $scope.clicked = false;
					// $state.go("dashboard.home");
				}
			});
		};

		$scope.addCourse = function() {
			console.log(this.courseid);

			StudentCourseService.addCourse(serializeData({
					couId: this.courseid
				}),
				function(data) {
					console.log(data);
					if (data.success) {
						toastr.success(data.message);
					} else {
						toastr.warning(data.message);
					}
					$state.go("course.list");
				});
		};

		$scope.addLocate = function() {
			if ($rootScope.curcourse == undefined) {
				toastr.warning('课程信息错误');
				return;
			}
			// console.log($rootScope.curcourse);
			// console.log(this.locInfo);
			this.locInfo.couId = $rootScope.curcourse.couId;
			LocateInfoService.save(this.locInfo, function(result) {
				if (result.success) {
					toastr.success(result.message);
				} else {
					toastr.error(result.message);
				}
			});
		};

		QuestionInfoService.getAllOrderByGood({
			couId: 0,
			pageNum: 0
		}, function(tdGoodQuestionBlocks) {
			console.log(tdGoodQuestionBlocks);
			$scope.tdGoodQuestionBlocks = tdGoodQuestionBlocks;
		});

		QuestionInfoService.getQuestionBySelectedCourse({
			couId: 0,
			pageNum: 0
		}, function(selectedGoodQuestionBlocks) {
			console.log(selectedGoodQuestionBlocks);
			$scope.selectedGoodQuestionBlocks = selectedGoodQuestionBlocks;
		});
	});